|
1
|
|
|
/* |
|
2
|
|
|
* Copyright (c) 2017-2018 Rafael da Silva Rocha. |
|
3
|
|
|
* https://github.com/rochars/byte-data |
|
4
|
|
|
* |
|
5
|
|
|
*/ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* @type {!Object} |
|
9
|
|
|
* @private |
|
10
|
|
|
*/ |
|
11
|
|
|
const packer = require("./lib/packer"); |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Write a number or fixed-length string to a byte buffer. |
|
15
|
|
|
* @param {number|string} value The value. |
|
16
|
|
|
* @param {!Object} theType The type definition. |
|
17
|
|
|
* @return {!Array<number|string>} |
|
18
|
|
|
* @throws {Error} If the type definition is not valid. |
|
19
|
|
|
*/ |
|
20
|
|
|
function pack(value, theType) { |
|
21
|
|
|
packer.setUp(theType); |
|
22
|
|
|
return packer.toBytes( |
|
23
|
|
|
[packer.fixBadString(value, theType)], theType); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Read a number or a fixed-length string from a byte buffer. |
|
28
|
|
|
* @param {!Array<number|string>|!Uint8Array} buffer An array of bytes. |
|
29
|
|
|
* @param {!Object} theType The type definition. |
|
30
|
|
|
* @return {number|string|null} |
|
31
|
|
|
* @throws {Error} If the type definition is not valid. |
|
32
|
|
|
*/ |
|
33
|
|
|
function unpack(buffer, theType) { |
|
34
|
|
|
packer.setUp(theType); |
|
35
|
|
|
let values = packer.fromBytes( |
|
36
|
|
|
buffer.slice(0, theType["offset"]), theType); |
|
37
|
|
|
return values ? values[0] : theType["char"] ? "" : null; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Write an array of numbers or a string to a byte buffer. |
|
42
|
|
|
* @param {!Array<number|string>} values The values. |
|
43
|
|
|
* @param {!Object} theType The type definition. |
|
44
|
|
|
* @return {!Array<number|string>} |
|
45
|
|
|
* @throws {Error} If the type definition is not valid. |
|
46
|
|
|
*/ |
|
47
|
|
|
function packArray(values, theType) { |
|
48
|
|
|
packer.setUp(theType); |
|
49
|
|
|
// Fix strings with bad length in the array |
|
50
|
|
|
if (theType["char"]) { |
|
51
|
|
|
let len = values.length; |
|
52
|
|
|
for (let i=0; i<len; i++) { |
|
53
|
|
|
values[i] = packer.fixBadString(values[i], theType); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
return packer.toBytes(values, theType); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Read an array of numbers or a string from a byte buffer. |
|
61
|
|
|
* @param {!Array<number|string>|!Uint8Array} buffer The byte array. |
|
62
|
|
|
* @param {!Object} theType The type definition. |
|
63
|
|
|
* @return {!Array<number|string>|number} |
|
64
|
|
|
* @throws {Error} If the type definition is not valid. |
|
65
|
|
|
*/ |
|
66
|
|
|
function unpackArray(buffer, theType) { |
|
67
|
|
|
packer.setUp(theType); |
|
68
|
|
|
return packer.fromBytes(buffer, theType); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
// Methods |
|
72
|
|
|
module.exports.pack = pack; |
|
73
|
|
|
module.exports.unpack = unpack; |
|
74
|
|
|
module.exports.packArray = packArray; |
|
75
|
|
|
module.exports.unpackArray = unpackArray; |
|
76
|
|
|
module.exports.types = require("./lib/types"); |
|
77
|
|
|
|